#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"


void SListPrint(SLTNote* phead)
{
	SLTNote* cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

SLTNote* SListCreat(SLDataType x)
{
	SLTNote* newnode = (SLTNote*)malloc(sizeof(SLTNote));
	assert(newnode);
	newnode->data = x;
	newnode->next = NULL;

	return newnode;
}


void SListPushBack(SLTNote** phead, SLDataType x)
{
	SLTNote* newnode = SListCreat(x);
	
	if (*phead == NULL)
	{
		*phead = newnode;
	}
	else 
	{

		SLTNote* tail = *phead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
	
}

void SListPushFront(SLTNote** phead, SLDataType x)
{
	SLTNote* newnode = SListCreat(x);
	newnode->next = *phead;
	*phead = newnode;
}

void SListPopBack(SLTNote** phead)
{
	assert(*phead);
	if ((*phead)->next==NULL)
	{
		free(*phead);
		(*phead)->next = NULL;
	}
	else
	{
		SLTNote* tail = *phead;
		SLTNote* tailprev = NULL;
		while (tail->next != NULL)
		{
			tailprev = tail;
			tail = tail->next;
		
		}
	
	free(tail);
	tailprev->next = NULL;

	}
	
}
void SListPopFront(SLTNote** phead)
{
	assert(*phead);
	SLTNote* newnode = (*phead)->next;
	free(*phead);
	*phead = newnode;
}


SLTNote* SListFind(SLTNote* phead, SLDataType x)
{
	SLTNote* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

void SListInsert(SLTNote** pphead, SLTNote* pos, SLDataType x)
{
	assert(pos);
	assert(pphead);
	if (pos == *pphead)
	{
		SListPushFront(pphead, x);
	}
	else
	{
		SLTNote* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		SLTNote* newnode = SListCreat(x);
		prev->next = newnode;
		newnode->next = pos;
		
	}

}
void SListErase(SLTNote** pphead, SLTNote* pos)
{
	assert(pos);
	assert(pphead);
	if (pos == *pphead)
	{
		SListPopFront(pphead);
	}
	else
	{
		SLTNote* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		prev->next = pos->next;
		free(pos);
	}
}

